const router = require('koa-router')()
const { exec } = require('../db/index')

// router.prefix('/')

router.get('/', function (ctx, next) {
  ctx.body = 'this is a users response!'
})

router.get('/bar', function (ctx, next) {
  ctx.body = 'this is a users/bar response'
})

// 登陆接口
router.get('/api/login', async (ctx, next) => {
  const { username, password } = ctx.query;
  const result = await exec(`
  select id, username, avatar from  users where
  username=? and password=?`,
  [username, password ])
  ctx.body = result
})

router.get('/api/test', async (ctx) => {
  const data = await exec(`select * from records left join users on records.submitId = users.id`)
  ctx.body = data;
})

// 获取用户信息接口
router.get('/api/getRecords', async (ctx) => {
  const { userId, brokerId } = ctx.query;
  const result = await exec(`
      select

      records.id,
      users.username as submit,
      users.avatar,
      records.content,
      records.brokerId,
      records.userId,
      records.submitId
      from records left join users on records.submitId = users.id where
  userId=? and brokerId=?`,
  [userId, brokerId])

  ctx.body = result;
})


module.exports = router
